程序运行时,需要输入抖音UP主的URL。例如:在电脑端,通过官网搜索,可以找到:XXX的主页,https://www.douyin.com/user/xxxxxxxxxxxxxxxxxx
源代码没有做容错处理,也没有针对抖音的反爬措施进行处理,因此运行中途可能会意外中止。# 定义函数get_video_ids(author_url),返回UP主全部短视频的ID的列表# 参数author_url:抖音UP主的主页# 例如,XXX的主页 https://www.douyin.com/user/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdef get_video_ids(author_url): ids = list() count = 0 retry = 0 n = 0 flag = True chrome_option = webdriver.ChromeOptions() chrome_option.add_argument('headless') # 静默模式 driver = webdriver.Chrome(options=chrome_option) driver.get(author_url) while flag and retry <= 15: driver.execute_script("window.scrollBy(0,2000)") # scrollBy(x, y),JavaScript中操作内容滚动指定的像素数 n = n 1 time.sleep(2) html_source = driver.page_source items = etree.HTML(html_source).xpath("//li[@class='ECMy_Zdt']") count_items = len(items) print("操作页面内容滚动{0:0>3}次后,获取视频ID{1:0>4}个。".format(n, count_items)) if count_items != count: count = count_items else: if retry < 15: retry = retry 1 else: flag = False print("已经达到可获取视频ID的最大数量,开始逐个获取视频ID:\n") for item in items: video_id = item.xpath("a/@href")[0].split("/")[-1] print("获取短视频ID:{}".format(video_id)) ids.append(video_id) return ids
评论